package com.fh.controller;

import com.fh.model.Orders;
import com.fh.quartz.MyPayConfig;
import com.fh.service.OrderSubmitService;
import com.fh.utils.ResultState;
import com.fh.vo.ResultVo;
import com.github.wxpay.sdk.WXPay;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("submit")
@CrossOrigin
public class OrderSubmitController {

   @Resource
   private OrderSubmitService orderSubmitService;


    @PostMapping("add")
    public ResultVo addOrders(String cids, @RequestBody Orders orders) throws Exception {

        Map<String, String> map1 = orderSubmitService.addOrders(cids, orders);


        if (map1.get("orderId")!=null){
            Map<String,String> map=new HashMap<>();
            map.put("body",map1.get("productName"));//商品描述
            map.put("out_trade_no",map1.get("orderId"));//订单编号
            map.put("fee_type","CNY");//支付类型
            map.put("total_fee","1");//支付金额
            map.put("trade_type","NATIVE");//交易类型
            map.put("notify_url","http://z7vmyj.natappfree.cc/pay/callback");//回调接口

            WXPay wxPay = new WXPay(new MyPayConfig());
            Map<String, String> resp = wxPay.unifiedOrder(map);
            map1.put("code_url",resp.get("code_url"));
            return  new  ResultVo(ResultState.OK,"ok",map1);
        }

        return  new  ResultVo(ResultState.NO,"no",null);

    }
}
